home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / BeOS / spheremap.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-03  |  19.2 KB  |  1,113 lines

  1. /* generic.c */
  2.  
  3. /* Demo of BEOS Mesa rendering */
  4.  
  5. /*
  6.  * See Mesa/include/GL/osmesa.h for documentation of the OSMesa functions.
  7.  *
  8.  * If you want to render BIG images you'll probably have to increase
  9.  * MAX_WIDTH and MAX_HEIGHT in src/config.h.
  10.  *
  11.  * This program is in the public domain.
  12.  *
  13.  * BEOS output provided by Tinic Urou
  14.  * 5uro@informatik.uni-hamburg.de
  15.  */
  16.  
  17. #include <AppKit.h>
  18. #include <InterfaceKit.h>
  19. #include <KernelKit.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <math.h>
  24. #include "GL/osmesa.h"
  25. #include "GL/glu.h"
  26. #include "gltk.h"
  27.  
  28. #define FALSE 0
  29. #define TRUE  1
  30. #ifndef PI
  31. #define PI    3.14159265358979323846
  32. #endif
  33.  
  34. GLenum doubleBuffer, directRender;
  35. int W = 400, H = 400;
  36.  
  37. char *imageFileName = "1.rgb";
  38. TK_RGBImageRec *image;
  39.  
  40. int numComponents;
  41.  
  42. float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
  43. float decal[] = {GL_DECAL};
  44. float modulate[] = {GL_MODULATE};
  45. float repeat[] = {GL_REPEAT};
  46. float clamp[] = {GL_CLAMP};
  47. float nnearest[] = {GL_NEAREST};
  48. float linear[] = {GL_LINEAR};
  49. float nearest_mipmap_nearest[] = {GL_NEAREST_MIPMAP_NEAREST};
  50. float nearest_mipmap_linear[] = {GL_NEAREST_MIPMAP_LINEAR};
  51. float linear_mipmap_nearest[] = {GL_LINEAR_MIPMAP_NEAREST};
  52. float linear_mipmap_linear[] = {GL_LINEAR_MIPMAP_LINEAR};
  53. GLint sphereMap[] = {GL_SPHERE_MAP};
  54.  
  55. float xRotation = 0.0, yRotation = 0.0;
  56. float zTranslate = -4.0;
  57. GLenum autoRotate = (GLenum)TRUE;
  58. GLenum deepestColor = (GLenum)TK_GREEN;
  59. GLenum isLit = (GLenum)TRUE;
  60. GLenum isFogged = (GLenum)FALSE;
  61. float *textureEnvironment = modulate;
  62.  
  63. struct MipMap {
  64.     int width, height;
  65.     unsigned char *data;
  66. };
  67.  
  68. int cube, cage, cylinder, torus, genericObject;
  69.  
  70. float c[6][4][4][3] = {
  71.     {
  72.     {
  73.         {
  74.         1.0, 1.0, -1.0
  75.         }, 
  76.         {
  77.         0.0, 1.0, -1.0
  78.         },
  79.         {
  80.         0.0, 0.0, -1.0
  81.         },
  82.         {
  83.         1.0, 0.0, -1.0
  84.         },
  85.     },
  86.     {
  87.         {
  88.         0.0, 1.0, -1.0
  89.         },
  90.         {
  91.         -1.0, 1.0, -1.0
  92.         }, 
  93.         {
  94.         -1.0, 0.0, -1.0
  95.         }, 
  96.         {
  97.         0.0, 0.0, -1.0
  98.         },
  99.     },
  100.     {
  101.         {
  102.         0.0,  0.0, -1.0
  103.         },
  104.         {
  105.         -1.0, 0.0, -1.0
  106.         },
  107.         {
  108.         -1.0, -1.0, -1.0
  109.         },
  110.         {
  111.         0.0, -1.0, -1.0
  112.         },
  113.     },
  114.     {
  115.         {
  116.         1.0, 0.0, -1.0
  117.         },
  118.         {
  119.         0.0, 0.0, -1.0
  120.         },
  121.         {
  122.         0.0, -1.0, -1.0
  123.         },
  124.         {
  125.         1.0, -1.0, -1.0
  126.         },
  127.     },
  128.     },
  129.     {
  130.     {
  131.         {
  132.         1.0, 1.0, 1.0
  133.         },
  134.         {
  135.         1.0, 1.0, 0.0
  136.         },
  137.         {
  138.         1.0, 0.0, 0.0
  139.         },
  140.         {
  141.         1.0, 0.0, 1.0
  142.         },
  143.     },
  144.     {
  145.         {
  146.         1.0, 1.0, 0.0
  147.         },
  148.         {
  149.         1.0, 1.0, -1.0
  150.         },
  151.         {
  152.         1.0, 0.0, -1.0
  153.         },
  154.         {
  155.         1.0, 0.0, 0.0
  156.         },
  157.     },
  158.     {
  159.         {
  160.         1.0, 0.0, -1.0
  161.         },
  162.         {
  163.         1.0, -1.0, -1.0
  164.         },
  165.         {
  166.         1.0, -1.0, 0.0
  167.         },
  168.         {
  169.         1.0, 0.0, 0.0
  170.         },
  171.     },
  172.     {
  173.         {
  174.         1.0, 0.0, 0.0
  175.         },
  176.         {
  177.         1.0, -1.0, 0.0
  178.         },
  179.         {
  180.         1.0, -1.0, 1.0
  181.         },
  182.         {
  183.         1.0, 0.0, 1.0
  184.         },
  185.     },
  186.     },
  187.     {
  188.     {
  189.         {
  190.         -1.0, 1.0, 1.0
  191.         },
  192.         {
  193.         0.0, 1.0, 1.0
  194.         },
  195.         {
  196.         0.0, 0.0, 1.0
  197.         },
  198.         {
  199.         -1.0, 0.0, 1.0
  200.         },
  201.     },
  202.     {
  203.         {
  204.         0.0, 1.0, 1.0
  205.         },
  206.         {
  207.         1.0, 1.0, 1.0
  208.         },
  209.         {
  210.         1.0, 0.0, 1.0
  211.         },
  212.         {
  213.         0.0, 0.0, 1.0
  214.         },
  215.     },
  216.     {
  217.         {
  218.         1.0, 0.0, 1.0
  219.         },
  220.         {
  221.         1.0, -1.0, 1.0
  222.         },
  223.         {
  224.         0.0, -1.0, 1.0
  225.         },
  226.         {
  227.         0.0, 0.0, 1.0
  228.         },
  229.     },
  230.     {
  231.         {
  232.         0.0, -1.0, 1.0
  233.         },
  234.         {
  235.         -1.0, -1.0, 1.0
  236.         },
  237.         {
  238.         -1.0, 0.0, 1.0
  239.         },
  240.         {
  241.         0.0, 0.0, 1.0
  242.         },
  243.     },
  244.     },
  245.     {
  246.     {
  247.         {
  248.         -1.0, 1.0, -1.0
  249.         },
  250.         {
  251.         -1.0, 1.0, 0.0
  252.         },
  253.         {
  254.         -1.0, 0.0, 0.0
  255.         },
  256.         {
  257.         -1.0, 0.0, -1.0
  258.         },
  259.     }, 
  260.     {
  261.         {
  262.         -1.0, 1.0, 0.0
  263.         },
  264.         {
  265.         -1.0, 1.0, 1.0
  266.         },
  267.         {
  268.         -1.0, 0.0, 1.0
  269.         },
  270.         {
  271.         -1.0, 0.0, 0.0
  272.         },
  273.     }, 
  274.     {
  275.         {
  276.         -1.0, 0.0, 1.0
  277.         },
  278.         {
  279.         -1.0, -1.0, 1.0
  280.         },
  281.         {
  282.         -1.0, -1.0, 0.0
  283.         },
  284.         {
  285.         -1.0, 0.0, 0.0
  286.         },
  287.     }, 
  288.     {
  289.         {
  290.         -1.0, -1.0, 0.0
  291.         },
  292.         {
  293.         -1.0, -1.0, -1.0
  294.         },
  295.         {
  296.         -1.0, 0.0, -1.0
  297.         },
  298.         {
  299.         -1.0, 0.0, 0.0
  300.         },
  301.     }, 
  302.     },
  303.     {
  304.     {
  305.         {
  306.         -1.0, 1.0, 1.0
  307.         },
  308.         {
  309.         -1.0, 1.0, 0.0
  310.         },
  311.         {
  312.         0.0, 1.0, 0.0
  313.         },
  314.         {
  315.         0.0, 1.0, 1.0
  316.         },
  317.     },
  318.     {
  319.         {
  320.         -1.0, 1.0, 0.0
  321.         },
  322.         {
  323.         -1.0, 1.0, -1.0
  324.         },
  325.         {
  326.         0.0, 1.0, -1.0
  327.         },
  328.         {
  329.         0.0, 1.0, 0.0
  330.         },
  331.     },
  332.     {
  333.         {
  334.         0.0, 1.0, -1.0
  335.         },
  336.         {
  337.         1.0, 1.0, -1.0
  338.         },
  339.         {
  340.         1.0, 1.0, 0.0
  341.         },
  342.         {
  343.         0.0, 1.0, 0.0
  344.         },
  345.     },
  346.     {
  347.         {
  348.         1.0, 1.0, 0.0
  349.         },
  350.         {
  351.         1.0, 1.0, 1.0
  352.         },
  353.         {
  354.         0.0, 1.0, 1.0
  355.         },
  356.         {
  357.         0.0, 1.0, 0.0
  358.         },
  359.     },
  360.     },
  361.     {
  362.     {
  363.         {
  364.         -1.0, -1.0, -1.0
  365.         },
  366.         {
  367.         -1.0, -1.0, 0.0
  368.         },
  369.         {
  370.         0.0, -1.0, 0.0
  371.         },
  372.         {
  373.         0.0, -1.0, -1.0
  374.         },
  375.     },
  376.     {
  377.         {
  378.         -1.0, -1.0, 0.0
  379.         },
  380.         {
  381.         -1.0, -1.0, 1.0
  382.         },
  383.         {
  384.         0.0, -1.0, 1.0
  385.         },
  386.         {
  387.         0.0, -1.0, 0.0
  388.         },
  389.     },
  390.     {
  391.         {
  392.         0.0, -1.0, 1.0
  393.         },
  394.         {
  395.         1.0, -1.0, 1.0
  396.         },
  397.         {
  398.         1.0, -1.0, 0.0
  399.         },
  400.         {
  401.         0.0, -1.0, 0.0
  402.         },
  403.     },
  404.     {
  405.         {
  406.         1.0, -1.0, 0.0
  407.         },
  408.         {
  409.         1.0, -1.0, -1.0
  410.         },
  411.         {
  412.         0.0, -1.0, -1.0
  413.         },
  414.         {
  415.         0.0, -1.0, 0.0
  416.         },
  417.     },
  418.     }
  419. };
  420.  
  421. float n[6][3] = {
  422.     {
  423.     0.0, 0.0, -1.0
  424.     },
  425.     {
  426.     1.0, 0.0, 0.0
  427.     },
  428.     {
  429.     0.0, 0.0, 1.0
  430.     },
  431.     {
  432.     -1.0, 0.0, 0.0
  433.     },
  434.     {
  435.     0.0, 1.0, 0.0
  436.     },
  437.     {
  438.     0.0, -1.0, 0.0
  439.     }
  440. };
  441.  
  442. GLfloat identity[16] = {
  443.     1, 0, 0, 0,
  444.     0, 1, 0, 0,
  445.     0, 0, 1, 0,
  446.     0, 0, 0, 1,
  447. };
  448.  
  449.  
  450. void BuildCylinder(int numEdges)
  451. {
  452.     int i, top = 1.0, bottom = -1.0;
  453.     float x[100], y[100], angle; 
  454.     
  455.     for (i = 0; i <= numEdges; i++) {
  456.     angle = i * 2.0 * PI / numEdges;
  457.     x[i] = cos(angle);   /* was cosf() */
  458.     y[i] = sin(angle);   /* was sinf() */
  459.     }
  460.  
  461.     glNewList(cylinder, GL_COMPILE);
  462.     glBegin(GL_TRIANGLE_STRIP);
  463.     for (i = 0; i <= numEdges; i++) {
  464.         glNormal3f(x[i], y[i], 0.0);
  465.         glVertex3f(x[i], y[i], bottom);
  466.         glVertex3f(x[i], y[i], top);
  467.     }
  468.     glEnd();
  469.     glBegin(GL_TRIANGLE_FAN);
  470.     glNormal3f(0.0, 0.0, 1.0);
  471.     glVertex3f(0.0, 0.0, top);
  472.     for (i = 0; i <= numEdges; i++) {
  473.         glVertex3f(x[i], -y[i], top);
  474.     }
  475.     glEnd();
  476.     glBegin(GL_TRIANGLE_FAN);
  477.     glNormal3f(0.0, 0.0, -1.0);
  478.     glVertex3f(0.0, 0.0, bottom);
  479.     for (i = 0; i <= numEdges; i++) {
  480.         glVertex3f(x[i], y[i], bottom);
  481.     }
  482.     glEnd();
  483.     glEndList();
  484. }
  485.  
  486. void BuildTorus(float rc, int numc, float rt, int numt)
  487. {
  488.     int i, j, k;
  489.     double s, t;
  490.     double x, y, z;
  491.     double pi, twopi;
  492.  
  493.     pi = 3.14159265358979323846;
  494.     twopi = 2.0 * pi;
  495.  
  496.     glNewList(torus, GL_COMPILE);
  497.     for (i = 0; i < numc; i++) {
  498.     glBegin(GL_QUAD_STRIP);
  499.         for (j = 0; j <= numt; j++) {
  500.         for (k = 0; k <= 1; k++) {
  501.         s = (i + k) % numc + 0.5;
  502.         t = j % numt;
  503.  
  504.         x = cos(t*twopi/numt) * cos(s*twopi/numc);
  505.         y = sin(t*twopi/numt) * cos(s*twopi/numc);
  506.         z = sin(s*twopi/numc);
  507.         glNormal3f(x, y, z);
  508.  
  509.         x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt);
  510.         y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt);
  511.         z = rc * sin(s*twopi/numc);
  512.         glVertex3f(x, y, z);
  513.         }
  514.         }
  515.     glEnd();
  516.     }
  517.     glEndList();
  518. }
  519.  
  520. void BuildCage(void)
  521. {
  522.     int i, j;
  523.     float inc;
  524.     float right, left, top, bottom, front, back;
  525.  
  526.     front  = 0.0;
  527.     back   = -8.0;
  528.  
  529.     left   = -4.0;
  530.     bottom = -4.0;
  531.     right  = 4.0;
  532.     top    = 4.0; 
  533.  
  534.     inc = 2.0 * 4.0 * 0.1;
  535.  
  536.     glNewList(cage, GL_COMPILE);
  537.     for (i = 0; i < 10; i++) {
  538.  
  539.     /*
  540.     ** Back
  541.     */
  542.     glBegin(GL_LINES);
  543.         glVertex3f(left+i*inc, top,    back);
  544.         glVertex3f(left+i*inc, bottom, back);
  545.     glEnd();
  546.     glBegin(GL_LINES);
  547.         glVertex3f(right, bottom+i*inc, back);
  548.         glVertex3f(left,  bottom+i*inc, back);
  549.     glEnd();
  550.  
  551.     /*
  552.     ** Front
  553.     */
  554.     glBegin(GL_LINES);
  555.         glVertex3f(left+i*inc, top,    front);
  556.         glVertex3f(left+i*inc, bottom, front);
  557.     glEnd();
  558.     glBegin(GL_LINES);
  559.         glVertex3f(right, bottom+i*inc, front);
  560.         glVertex3f(left,  bottom+i*inc, front);
  561.     glEnd();
  562.  
  563.     /*
  564.     ** Left
  565.     */
  566.     glBegin(GL_LINES);
  567.         glVertex3f(left, bottom+i*inc, front);
  568.         glVertex3f(left, bottom+i*inc, back);
  569.     glEnd();
  570.     glBegin(GL_LINES);
  571.         glVertex3f(left, top,    back+i*inc);
  572.         glVertex3f(left, bottom, back+i*inc);
  573.     glEnd();
  574.  
  575.     /*
  576.     ** Right
  577.     */
  578.     glBegin(GL_LINES);
  579.         glVertex3f(right, top-i*inc, front);
  580.         glVertex3f(right, top-i*inc, back);
  581.     glEnd();
  582.     glBegin(GL_LINES);
  583.         glVertex3f(right, top,    back+i*inc);
  584.         glVertex3f(right, bottom, back+i*inc);
  585.     glEnd();
  586.  
  587.     /*
  588.     ** Top
  589.     */
  590.     glBegin(GL_LINES);
  591.         glVertex3f(left+i*inc, top, front);
  592.         glVertex3f(left+i*inc, top, back);
  593.     glEnd();
  594.     glBegin(GL_LINES);
  595.         glVertex3f(right, top, back+i*inc);
  596.         glVertex3f(left,  top, back+i*inc);
  597.     glEnd();
  598.  
  599.     /*
  600.     ** Bottom
  601.     */
  602.     glBegin(GL_LINES);
  603.         glVertex3f(right-i*inc, bottom, front);
  604.         glVertex3f(right-i*inc, bottom, back);
  605.     glEnd();
  606.     glBegin(GL_LINES);
  607.         glVertex3f(right, bottom, back+i*inc);
  608.         glVertex3f(left,  bottom, back+i*inc);
  609.     glEnd();
  610.     }
  611.     glEndList();
  612. }
  613.  
  614. void BuildCube(void)
  615. {
  616.     int i, j;
  617.  
  618.     glNewList(cube, GL_COMPILE);
  619.     for (i = 0; i < 6; i++) {
  620.     for (j = 0; j < 4; j++) {
  621.         glNormal3fv(n[i]); 
  622.         glBegin(GL_POLYGON);
  623.         glVertex3fv(c[i][j][0]);
  624.         glVertex3fv(c[i][j][1]);
  625.         glVertex3fv(c[i][j][2]);
  626.         glVertex3fv(c[i][j][3]);
  627.         glEnd();
  628.     }
  629.     }
  630.     glEndList();
  631. }
  632.  
  633. void BuildLists(void)
  634. {
  635.  
  636.     cube = glGenLists(1);
  637.     BuildCube();
  638.  
  639.     cage = glGenLists(2);
  640.     BuildCage();
  641.  
  642.     cylinder = glGenLists(3);
  643.     BuildCylinder(60);
  644.  
  645.     torus = glGenLists(4);
  646.     BuildTorus(0.65, 20, .85, 65);
  647.  
  648.     genericObject = torus;
  649. }
  650.  
  651. void SetDeepestColor(void)
  652. {
  653.     GLint redBits, greenBits, blueBits;
  654.  
  655.     glGetIntegerv(GL_RED_BITS, &redBits);
  656.     glGetIntegerv(GL_GREEN_BITS, &greenBits);
  657.     glGetIntegerv(GL_BLUE_BITS, &blueBits);
  658.  
  659.     deepestColor = (GLenum)((redBits >= greenBits) ? TK_RED : TK_GREEN);
  660.     deepestColor = (GLenum)((deepestColor >= blueBits) ? deepestColor : TK_BLUE); 
  661. }
  662.  
  663. void SetDefaultSettings(void)
  664. {
  665.  
  666.     magFilter = nnearest;
  667.     minFilter = nnearest;
  668.     sWrapMode = repeat;
  669.     tWrapMode = repeat;
  670.     textureEnvironment = modulate;
  671.     autoRotate = (GLenum)TRUE;
  672. }
  673.  
  674. unsigned char *AlphaPadImage(int bufSize, unsigned char *inData, int alpha)
  675. {
  676.     unsigned char *outData, *out_ptr, *in_ptr;
  677.     int i;
  678.  
  679.     outData = (unsigned char *) malloc(bufSize * 4);
  680.     out_ptr = outData;
  681.     in_ptr = inData;
  682.  
  683.     for (i = 0; i < bufSize; i++) {
  684.     *out_ptr++ = *in_ptr++;
  685.     *out_ptr++ = *in_ptr++;
  686.     *out_ptr++ = *in_ptr++;
  687.     *out_ptr++ = alpha;
  688.     }
  689.  
  690.     free (inData);
  691.     return outData;
  692. }
  693.  
  694. void Init(void)
  695. {
  696.     float ambient[] = {0.0, 0.0, 0.0, 1.0};
  697.     float diffuse[] = {0.0, 1.0, 0.0, 1.0};
  698.     float specular[] = {1.0, 1.0, 1.0, 1.0};
  699.     float position[] = {2.0, 2.0,  0.0, 1.0};
  700.     float fog_color[] = {0.0, 0.0, 0.0, 1.0};
  701.     float mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
  702.     float mat_shininess[] = {90.0};
  703.     float mat_specular[] = {1.0, 1.0, 1.0, 1.0};
  704.     float mat_diffuse[] = {1.0, 1.0, 1.0, 1.0};
  705.     float lmodel_ambient[] = {0.0, 0.0, 0.0, 1.0};
  706.     float lmodel_twoside[] = {GL_TRUE};
  707.     float lmodel_local_viewer[] = {GL_FALSE};
  708.  
  709.     SetDeepestColor();
  710.     SetDefaultSettings();
  711.  
  712.     if (numComponents == 4) {
  713.     image = tkRGBImageLoad(imageFileName);
  714.     image->data = AlphaPadImage(image->sizeX*image->sizeY,
  715.                                     image->data, 128);
  716.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  717.     gluBuild2DMipmaps(GL_TEXTURE_2D, numComponents, 
  718.               image->sizeX, image->sizeY, 
  719.               GL_RGBA, GL_UNSIGNED_BYTE, image->data);
  720.     } else {
  721.     image = tkRGBImageLoad(imageFileName);
  722.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  723.     gluBuild2DMipmaps(GL_TEXTURE_2D, numComponents, 
  724.               image->sizeX, image->sizeY, 
  725.               GL_RGB, GL_UNSIGNED_BYTE, image->data);
  726.     }
  727.     
  728.     glFogf(GL_FOG_DENSITY, 0.125);
  729.     glFogi(GL_FOG_MODE, GL_LINEAR);
  730.     glFogf(GL_FOG_START, 4.0);
  731.     glFogf(GL_FOG_END, 9.0);
  732.     glFogfv(GL_FOG_COLOR, fog_color);
  733.  
  734.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  735.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  736.     glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
  737.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  738.     
  739.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  740.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  741.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  742.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  743.  
  744.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  745.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  746.     glShadeModel(GL_SMOOTH);
  747.  
  748.     glEnable(GL_LIGHTING);
  749.     glEnable(GL_LIGHT0);
  750.  
  751.     glClearColor(0.0, 0.0, 0.0, 0.0);
  752.     glViewport(0, 0, W, H);
  753.     glEnable(GL_DEPTH_TEST);
  754.  
  755.     glFrontFace(GL_CW);
  756.     glEnable(GL_CULL_FACE);
  757.     glCullFace(GL_BACK);
  758.  
  759.     glEnable(GL_TEXTURE_2D);
  760.     glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap);
  761.     glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap);
  762.     glEnable(GL_TEXTURE_GEN_S);
  763.     glEnable(GL_TEXTURE_GEN_T);
  764.  
  765.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  766.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  767.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
  768.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
  769.  
  770.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textureEnvironment);
  771.  
  772.     BuildLists();
  773. }
  774.  
  775. void ReInit(void)
  776. {
  777.  
  778.     if (genericObject == torus) {
  779.     glEnable(GL_DEPTH_TEST);
  780.     } else  {
  781.     glDisable(GL_DEPTH_TEST);
  782.     }
  783.     if (isFogged) {
  784.     textureEnvironment = modulate;
  785.     }
  786.  
  787.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  788.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  789.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textureEnvironment);
  790. }
  791.  
  792. void Draw(void)
  793. {
  794.  
  795.     glMatrixMode(GL_PROJECTION);
  796.     glLoadIdentity();
  797.     glFrustum(-0.2, 0.2, -0.2, 0.2, 0.15, 9.0);
  798.     glMatrixMode(GL_MODELVIEW);
  799.  
  800.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  801.     if (isFogged) {
  802.     glEnable(GL_FOG);
  803.     glColor3fv(tkRGBMap[deepestColor]);
  804.     } else {
  805.     glColor3fv(tkRGBMap[TK_WHITE]);
  806.     }
  807.     glDisable(GL_LIGHTING);
  808.     glDisable(GL_LIGHT0);
  809.     glDisable(GL_TEXTURE_2D);
  810.     glCallList(cage);
  811.  
  812.     glPushMatrix();
  813.     glTranslatef(0.0, 0.0, zTranslate);
  814.     glRotatef(xRotation, 1, 0, 0);
  815.     glRotatef(yRotation, 0, 1, 0);
  816.  
  817.     if (isLit == TRUE) {
  818.     glEnable(GL_LIGHTING);
  819.     glEnable(GL_LIGHT0);
  820.     }
  821.  
  822.     glEnable(GL_TEXTURE_2D);
  823.     if (isFogged) {
  824.     glDisable(GL_FOG);
  825.     }
  826.     glPolygonMode(GL_FRONT, GL_FILL);
  827.     glColor3fv(tkRGBMap[deepestColor]);
  828.     glCallList(genericObject);
  829.  
  830.     glPopMatrix();
  831.     glFlush();
  832.  
  833.     if (autoRotate) {
  834.     xRotation += .75;
  835.     yRotation += .375;
  836.     } 
  837.     tkSwapBuffers();
  838. }
  839.  
  840. void Reshape(int width, int height)
  841. {
  842.  
  843.     W = width;
  844.     H = height;
  845.     ReInit();
  846.     glViewport( 0, 0, width, height );  /*new*/
  847. }
  848.  
  849. GLenum Args(int argc, char **argv)
  850. {
  851.     GLint i;
  852.  
  853.     doubleBuffer = GL_FALSE;
  854.     directRender = GL_TRUE;
  855.     numComponents = 4;
  856.  
  857.     for (i = 1; i < argc; i++) {
  858.     if (strcmp(argv[i], "-sb") == 0) {
  859.         doubleBuffer = GL_FALSE;
  860.     } else if (strcmp(argv[i], "-db") == 0) {
  861.         doubleBuffer = GL_TRUE;
  862.     } else if (strcmp(argv[i], "-dr") == 0) {
  863.         directRender = GL_TRUE;
  864.     } else if (strcmp(argv[i], "-ir") == 0) {
  865.         directRender = GL_FALSE;
  866.     } else if (strcmp(argv[i], "-f") == 0) {
  867.         if (i+1 >= argc || argv[i+1][0] == '-') {
  868.         printf("-f (No file name).\n");
  869.         return GL_FALSE;
  870.         } else {
  871.         imageFileName = argv[++i];
  872.         }
  873.     } else if (strcmp(argv[i], "-4") == 0) {
  874.         numComponents = 4;
  875.     } else if (strcmp(argv[i], "-3") == 0) {
  876.         numComponents = 3;
  877.     } else {
  878.         printf("%s (Bad option).\n", argv[i]);
  879.         return GL_FALSE;
  880.     }
  881.     }
  882.     return GL_TRUE;
  883. }
  884.  
  885. int                     gl_width=300,gl_height=300;
  886.  
  887. const     ulong             APP_SIGNATURE = '????';
  888. const     ulong            MSG_REDRAW = 1;
  889.  
  890. class                     MesaWindow;
  891. class                     MesaView;
  892. class                    MesaApp;
  893.  
  894. // Lets make our life easy and make them global:
  895.  
  896. BBitmap                 *the_bitmap;
  897. MesaView                *the_view;    
  898. MesaWindow                 *the_window;
  899.  
  900. class MesaView : public BView
  901. {
  902.     public:
  903.     
  904.     MesaView(BRect frame):BView(frame,"Mesa View",B_FOLLOW_NONE,B_WILL_DRAW)
  905.     {
  906.     };
  907.     
  908.     void KeyDown(ulong aKey)
  909.     {
  910.         switch(aKey)
  911.         {
  912.       case B_LEFT_ARROW:
  913.     yRotation -= 0.5;
  914.     autoRotate = (GLenum)FALSE;
  915.     ReInit();
  916.     break;
  917.       case B_RIGHT_ARROW:
  918.     yRotation += 0.5;
  919.     autoRotate = (GLenum)FALSE;
  920.     ReInit();
  921.     break;
  922.       case B_UP_ARROW:
  923.     xRotation -= 0.5;
  924.     autoRotate = (GLenum)FALSE;
  925.     ReInit();
  926.     break;
  927.       case B_DOWN_ARROW:
  928.     xRotation += 0.5;
  929.     autoRotate = (GLenum)FALSE;
  930.     ReInit();
  931.     break;
  932.       case 'a':
  933.     autoRotate = (GLenum)!autoRotate;
  934.     ReInit();
  935.     break;
  936.       case 'c':
  937.     genericObject = (GLenum)((genericObject == cube) ? cylinder : cube);
  938.     ReInit();
  939.     break;
  940.       case 'd':
  941.     textureEnvironment = decal;
  942.     ReInit();
  943.     break;
  944.       case 'm':
  945.     textureEnvironment = modulate;
  946.     ReInit();
  947.     break;
  948.       case 'l':
  949.     isLit = (GLenum)!isLit;
  950.     ReInit();
  951.     break;
  952.       case 'f':
  953.     isFogged = (GLenum)!isFogged;
  954.     ReInit();
  955.     break;
  956.       case 't':
  957.     genericObject = (GLenum)torus;
  958.     ReInit();
  959.     break;
  960.       case '0':
  961.     magFilter = nnearest;
  962.     ReInit();
  963.     break;
  964.       case '1':
  965.     magFilter = linear;
  966.     ReInit();
  967.     break;
  968.       case '2':
  969.     minFilter = nnearest;
  970.     ReInit();
  971.     break;
  972.       case '3':
  973.     minFilter = linear;
  974.     ReInit();
  975.     break;
  976.       case '4':
  977.     minFilter = nearest_mipmap_nearest;
  978.     ReInit();
  979.     break;
  980.       case '5':
  981.     minFilter = nearest_mipmap_linear;
  982.     ReInit();
  983.     break;
  984.       case '6':
  985.     minFilter = linear_mipmap_nearest;
  986.     ReInit();
  987.     break;
  988.       case '7':
  989.     minFilter = linear_mipmap_linear;
  990.     ReInit();
  991.     break;
  992.         }
  993.     };
  994.     
  995.     void Draw(BRect frame)
  996.     {
  997.         DrawBitmap(the_bitmap,BPoint(0,0));
  998.     };                    
  999. };
  1000.  
  1001. class MesaWindow : public BWindow 
  1002. {
  1003.     public:
  1004.     
  1005.     MesaWindow(int width, int height):BWindow(BRect(0,0,width-1,height-1),"MesaView",B_TITLED_WINDOW,B_NOT_RESIZABLE|B_NOT_ZOOMABLE)
  1006.     {
  1007.         // Move window to right position
  1008.         MoveTo(80, 24);
  1009.         // Create bitmap view
  1010.         Lock();
  1011.         AddChild(the_view = new MesaView(BRect(0, 0, (gl_width)-1, (gl_height)-1)));
  1012.         the_view->MakeFocus();
  1013.         Unlock();
  1014.     };
  1015.     
  1016.     void MessageReceived(BMessage *msg)
  1017.     {
  1018.         switch(msg->what)
  1019.         {    
  1020.             case    MSG_REDRAW:
  1021.                     Lock();
  1022.                     the_view->DrawBitmap(the_bitmap,BPoint(0,0));
  1023.                     Draw();
  1024.                     Unlock();
  1025.                     PostMessage(MSG_REDRAW);
  1026.                     break;
  1027.             default:
  1028.                     BWindow::MessageReceived(msg);
  1029.                     break;
  1030.         }
  1031.     };
  1032.     
  1033.     bool QuitRequested(void)
  1034.     {
  1035.         be_app->PostMessage(B_QUIT_REQUESTED);
  1036.         return TRUE;
  1037.     };
  1038. };
  1039.  
  1040. class MesaApp : public BApplication 
  1041. {
  1042.     OSMesaContext ctx;
  1043.     
  1044.     public:
  1045.     
  1046.     MesaApp():BApplication(APP_SIGNATURE)
  1047.     {
  1048.         the_window = NULL;
  1049.         the_bitmap = NULL;
  1050.     };
  1051.             
  1052.     void ReadyToRun(void)
  1053.     {
  1054.         // Allocate the bitmap        
  1055.         the_bitmap = new BBitmap(BRect(0, 0, gl_width-1, gl_height-1), B_RGB_32_BIT);
  1056.  
  1057.         uchar *bits = (uchar *)the_bitmap->Bits();
  1058.  
  1059.         memset(bits,0,the_bitmap->BytesPerRow()*gl_height);
  1060.  
  1061.         // Open window
  1062.         the_window = new MesaWindow((gl_width),(gl_height));
  1063.         the_window->Show();
  1064.  
  1065.         unsigned char *buffer;
  1066.         double start,end;
  1067.  
  1068.         // Create an RGBA-mode context 
  1069.         ctx = OSMesaCreateContext( GL_RGBA, NULL );
  1070.  
  1071.         // Bind the buffer to the context and make it current */
  1072.         OSMesaMakeCurrent( ctx, bits, GL_UNSIGNED_BYTE, gl_width, gl_height );
  1073.         OSMesaPixelStore( OSMESA_Y_UP, 0 );
  1074.  
  1075.         Init();
  1076.         Reshape(gl_width,gl_height);
  1077.         the_window->PostMessage(MSG_REDRAW);    
  1078.     };
  1079.  
  1080.     bool QuitRequested(void)
  1081.     {
  1082.         // Make sure that the window closes first
  1083.         if (BApplication::QuitRequested()) 
  1084.         {
  1085.             OSMesaDestroyContext( ctx );
  1086.             if (the_bitmap)    delete the_bitmap;
  1087.             return TRUE;
  1088.         }
  1089.         return FALSE;
  1090.     };
  1091.             
  1092.     void AboutRequested(void)
  1093.     {
  1094.         char str[256];
  1095.         sprintf(str, "MesaDemos, ported by Tinic Urou\n<5uro@informatik.uni-hamburg.de>\nFreely distributable.");
  1096.         BAlert *the_alert = new BAlert("", str, "OK");
  1097.         the_alert->Go();
  1098.     };
  1099. };
  1100.  
  1101. int main( int argc, char *argv[] )
  1102. {
  1103.     MesaApp *the_app;
  1104.  
  1105.     Args(argc,argv);
  1106.  
  1107.     the_app = new MesaApp();
  1108.     the_app->Run();
  1109.     delete the_app;
  1110.  
  1111.     return 0;
  1112. }
  1113.